﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QLTV.Forms.KhaiBao
{
    public partial class frmPhuongXa : BaseForm
    {
        public frmPhuongXa()
        {
            InitializeComponent();
        }

        public int PhuongXaId
        {
            get;
            set;
        }

        public int QuanHuyenId
        {
            get;
            set;
        }

        public int TinhThanhId
        {
            get;
            set;
        }

        private void frmPhuongXa_Load(object sender, EventArgs e)
        {
            tblPhuongXaTableAdapter.Fill(dsDiaChi.tblPhuongXa);
            // TODO: This line of code loads data into the 'dsDiaChi.tblTinhThanh' table. You can move, or remove it, as needed.
            this.tblTinhThanhTableAdapter.Fill(this.dsDiaChi.tblTinhThanh);
            // TODO: This line of code loads data into the 'dsDiaChi.tblQuanHuyen' table. You can move, or remove it, as needed.
            this.tblQuanHuyenTableAdapter.Fill(this.dsDiaChi.tblQuanHuyen);
            //tblQuanHuyenBindingSource.Filter = "TinhThanhID=0";

            if (this.PhuongXaId != 0)
            {
                tblPhuongXaBindingSource.MoveFirst();
                for (int i = 0; i < tblPhuongXaBindingSource.Count; i++)
                {
                    QLTV.Database.dsDiaChi.tblPhuongXaRow phuongxa = (QLTV.Database.dsDiaChi.tblPhuongXaRow)((DataRowView)tblPhuongXaBindingSource.Current).Row;
                    if (phuongxa.PhuongXaID == this.PhuongXaId)
                    {
                        break;
                    }
                    else
                    {
                        tblPhuongXaBindingSource.MoveNext();
                    }
                }
            }
            else
            {
                tblPhuongXaBindingSource.AddNew();
            }

            if (this.TinhThanhId != 0)
            {
                luTinhThanh.EditValue = this.TinhThanhId;
            }

            if (this.QuanHuyenId != 0)
            {
                luQuanHuyen.EditValue = this.QuanHuyenId;
            }

           

        }

        private void luTinhThanh_EditValueChanged(object sender, EventArgs e)
        {
            tblQuanHuyenBindingSource.Filter = String.Format("TinhThanhID='{0}'", (int)luTinhThanh.EditValue);
        }

        private void cmdSaveInsert_Click(object sender, EventArgs e)
        {

            updatePhuongXa();
            tblPhuongXaBindingSource.AddNew();

        }
        void updatePhuongXa()
        {
            if (dxValidationProvider1.Validate())
            {
                tblPhuongXaBindingSource.EndEdit();
                tblPhuongXaTableAdapter.Update(dsDiaChi.tblPhuongXa);
            }

        }

        private void cmdLuuVaDong_Click(object sender, EventArgs e)
        {
            updatePhuongXa();
            this.Close();
        }

        private void cmdDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}